package com.mryan.auth.controller;

import com.mryan.auth.pojo.Message;
import com.mryan.auth.service.AuthService;
import com.mryan.auth.util.QRCodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description： AuthController 二维码状态 待扫描、已扫描待确认、已确认;
 * @Author MRyan
 * @Date 2021/6/14 16:45
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "auth", method = {RequestMethod.GET, RequestMethod.POST})
public class AuthController {

    private Logger logger = LoggerFactory.getLogger(AuthController.class);

    @Autowired
    private AuthService authService;


    /**
     * 1. 获取QRCodeID 绑定地址 ip 将二维码ID返回给PC端
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/QRCodeId")
    @ResponseBody
    public Message getQRCodeId(HttpServletRequest request) {
        try {
            return authService.addAuthInfo(request);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return new Message(201, "生成并绑定二维码ID失败", null);
    }


    /**
     * 2. 根据QRCodeID生成二维码 PC端已二维码的形式显示二维码;
     *
     * @param qrCodeId
     * @param response
     */
    @RequestMapping(value = "/img/{qrCodeId}")
    public void getQRCodeImgByQRCodeID(@PathVariable("qrCodeId") String qrCodeId, HttpServletResponse response) {
        try {
            //生成包含deviceInfo的二维码
            QRCodeUtil.encode(qrCodeId, response);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    /**
     * 3. (待扫描) PC端携带设备信息向服务端发送请求,服务端生成二维码ID与PC设备信息进行绑定
     *
     * @param deviceInfo
     * @param request
     * @return
     */
    @RequestMapping(value = "/bindAuth/{qrCodeId}/{deviceInfo}")
    @ResponseBody
    public Message bindAuthByDeviceInfo(@PathVariable("qrCodeId") String qrCodeId,
                                        @PathVariable("deviceInfo") String deviceInfo) {
        try {
            Message message = authService.bindAuthByDeviceInfo(deviceInfo, qrCodeId);
            return message;
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return new Message(201, "设备信息绑定失败", null);
    }

    /**
     * 4. （已扫描待确认）移动端扫描PC端二维码,获取到二维码ID,移动端带二维码ID+PC端身份信息(deviceInfo)发送给服务端,
     * 服务端验证身份信息通过后,将二维码ID与身份信息绑定,并生成临时token返回给移动端,二维码状态变为已扫描待确认;
     *
     * @param accountName
     * @param qrCodeId
     * @param isScan
     * @return
     */
    @RequestMapping(value = "/temporaryToken/{accountName}")
    @ResponseBody
    public Message getTemporaryToken(@PathVariable("accountName") String accountName,
                                     @RequestParam("qrCodeId") String qrCodeId,
                                     boolean isScan) {
        return authService.getTemporaryToken(accountName, qrCodeId, isScan);
    }


    /**
     * 5. （已确认）移动端确认登录,并携带临时token请求服务端,服务端验证临时token通过后,改变二维码状态为已确认并生成PC token
     *
     * @param temporaryToken
     * @param accountName
     * @return
     */
    @RequestMapping(value = "/infoToken/{accountName}/{temporaryToken}/{qrCodeId}")
    @ResponseBody
    public Message getToken(@PathVariable("temporaryToken") String temporaryToken,
                            @PathVariable("qrCodeId") String qrCodeId,
                            @PathVariable("accountName") String accountName) {
        return authService.getToken(temporaryToken, accountName, qrCodeId);
    }

    /**
     * 6. PC 使用token进行验证状态
     * 0等待验证，1验证成功，2正在验证，3验证失败（过期）
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/accountInfo/{token}")
    @ResponseBody
    public Message getAccountInfoByToken(@PathVariable("token") String token) {
        return authService.getAccountInfoByToken(token);
    }

    /**
     * PC 使用qrCodeId进行验证状态
     * 0等待验证，1验证成功，2正在验证，3验证失败（过期）
     *
     * @param token
     * @param userId
     * @return
     */
    @RequestMapping(value = "/authInfo/{qrCodeId}")
    @ResponseBody
    public Message authInfo(@PathVariable("qrCodeId") String qrCodeId) {
        return authService.getAuthInfo(qrCodeId);
    }

}
